<?php
global $current_user;
get_currentuserinfo();
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);

if ($user_role == 'administrator' || $user_role == 'moderator'){
$comment = new Pod('comment');
$comment->findRecords("created ASC", -1,"approved='0'");
if($comment->getTotalRows() > 0){
  echo '<script type="text/javascript" language="javascript" src="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/jquery.dataTables.min.js"></script>';
  echo '<link rel="stylesheet" type="text/css" media="all" href="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/demo_page.css" />
  <link rel="stylesheet" type="text/css" media="all" href="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/demo_table_jui.css" />
  <link rel="stylesheet" type="text/css" media="all" href="'.BUZZR_WP_URL.'/wp-content/pfiles/datatables/jquery-ui-1.8.4.custom.css" />';
  
  function createDataTablesString($data_array, $columns_array){
    $datatable_columns_string = '';
    $datatable_data_string = '';
    
    foreach($columns_array as $columns)
    {
      $datatable_columns_string .= '{ ';
      foreach($columns as $key=>$value)
      {
        //$datatable_columns_string .= '"'.$key.'": "'.$value.'",';
        $datatable_columns_string .= $key.": '".$value."',";
      }
      $datatable_columns_string = substr($datatable_columns_string, 0, -1);
      $datatable_columns_string .= '},';
    }
    $datatable_columns_string = substr($datatable_columns_string, 0, -1);
    
    if(count($data_array))
    {
      $datatable_data_string = '[';
      foreach($data_array as $data)
      {
        $datatable_data_string .= '[';
        foreach($data as $key=>$value)
        {
          $datatable_data_string .= '\'' . $value . '\',';
        }
        $datatable_data_string = substr($datatable_data_string, 0, -1);
        $datatable_data_string .= '],';
      }
      $datatable_data_string = substr($datatable_data_string, 0, -1);
      $datatable_data_string .= ']';
    }
    
    $data['datatable_columns_string'] = $datatable_columns_string;
    $data['datatable_data_string'] = $datatable_data_string;
    
    return $data;
  }
  
  $columns_array[0]['sTitle'] = 'Deal';
  $columns_array[0]['sClass'] = 'center';
  $columns_array[0]['sWidth'] = '120px';
  $columns_array[1]['sTitle'] = 'Comment';
  $columns_array[1]['sClass'] = 'center';
  $columns_array[2]['sTitle'] = 'User';
  $columns_array[2]['sClass'] = 'center';
  $columns_array[2]['sWidth'] = '100px';
  $columns_array[3]['sTitle'] = 'Points';
  $columns_array[3]['sClass'] = 'center';
  $columns_array[3]['sWidth'] = '70px';
  $columns_array[4]['sTitle'] = 'Approve';
  $columns_array[4]['sClass'] = 'center';
  $columns_array[4]['sWidth'] = '50px';
  $columns_array[5]['sTitle'] = 'Delete';
  $columns_array[5]['sClass'] = 'center';
  $columns_array[5]['sWidth'] = '50px';
  
  $i = 0;
  while($comment->fetchRecord()){
    $data_array[$i][] = '<a href="'.BUZZR_URL.'/deals/'.$comment->get_field('associated_deal.camp_link').'">'.pods_sanitize(stripslashes($comment->get_field('associated_deal.name'))).'</a>';
    $data_array[$i][] = pods_sanitize(stripslashes(htmlentities($comment->get_field('comment'))));
    $data_array[$i][] = '<a href="'.BUZZR_URL.'/member/'.$comment->get_field('user.slug').'">'.$comment->get_field('user.name').'</a>';
    $total_points = $comment->get_field('user.points.registration') + $comment->get_field('user.points.login') + $comment->get_field('user.points.comments') + $comment->get_field('user.points.deals') + $comment->get_field('user.points.like_fb') + $comment->get_field('user.points.feedback') + $comment->get_field('user.points.vote');
    $data_array[$i][] = $total_points;
    $approve = $comment->get_field('id');
    $delete = $comment->get_field('id').','.$comment->get_field('user.id');
    $data_array[$i][] = '<a href="javascript:void(0);" id="app'.$approve.'" class="approve" value="'.$approve.'"><img src="'.BUZZR_WP_URL.'/wp-content/pfiles/images/approve.png" /></a>';
    $data_array[$i][] = '<a href="javascript:void(0);" id="del'.$approve.'" class="delete" value="'.$delete.'"><img src="'.BUZZR_WP_URL.'/wp-content/pfiles/images/delete.png" /></a>';
    $i++;
  }
  
  $data = createDataTablesString($data_array, $columns_array);
  $datatable_columns_string = $data['datatable_columns_string'];
  $datatable_data_string = $data['datatable_data_string'];
?>
<script type="text/javascript" charset="utf-8">
  
  var aDataSet = <?php echo $datatable_data_string ?>;
  jQuery(document).ready(function($) { 
    
    function fnGetSelected( oTableLocal ){
    	var aReturn = new Array();
    	var aTrs = oTableLocal.fnGetNodes();
    	
    	for( var i=0 ; i<aTrs.length ; i++ ){
    		if ( $(aTrs[i]).hasClass('row_selected') ){
    			aReturn.push( aTrs[i] );
    		}
    	}
    	return aReturn;
    }
      
    $('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
		var oTable = $('#example').dataTable( {
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                "aaData": aDataSet,
			"aoColumns": [
        <?php echo $datatable_columns_string ?>
			]
		});
    
    $('.approve').live('click',function(event) {
        var comment_id = $(this).attr('value');
        
        var id = $(this).parents("tr").attr('id');
        var action = $(this).attr('class');
        $(oTable.fnSettings().aoData).each(function (){
			   $(this.nTr).removeClass('row_selected');
			  });
        $(event.target).parents("tr").addClass('row_selected');
        $.ajax({ 
      	type: "post", 
      	url: "<?php echo BUZZR_WP_URL.'/wp-content/plugins/buzzr_ajax_comment/moderate.php'; ?>", data: "comment_id="+comment_id,
      	success: function(msg){
      	  var anSelected = fnGetSelected( oTable );
      		$(".result1").empty();   
      		$(".result1").append(msg);
      		oTable.fnDeleteRow(anSelected[0]);
      	  }
      	});  
    });
    
    $('.delete').live('click',function(event) {
        var ids = $(this).attr('value');
        var myArray = ids.split(',');
        
        var id = $(this).parents("tr").attr('id');
        var action = $(this).attr('class');
        $(oTable.fnSettings().aoData).each(function (){
			   $(this.nTr).removeClass('row_selected');
			  });
        $(event.target).parents("tr").addClass('row_selected');
        $.ajax({ 
      	type: "post", 
      	url: "<?php echo BUZZR_WP_URL.'/wp-content/plugins/buzzr_ajax_comment/moderate.php'; ?>", data: "ids="+ids,
      	success: function(msg){
          var anSelected = fnGetSelected( oTable );
          $(".result1").empty(); 
      		$(".result1").append(msg);
      		oTable.fnDeleteRow(anSelected[0]);
      	  }
      	});  
    });
    
    $('.loading').hide().ajaxStart(function() {
        $('.result1').hide();            
        $(this).show();
    }).ajaxStop(function() {
        $(this).hide();
        $('.result1').show();
    });
    
});
</script>
<div id="container" style="width: 90%; margin: 0 auto; float: none;">
<strong>Pending Comments for Moderation</strong>&nbsp;&nbsp;<span class="loading"><img src="<?php echo BUZZR_WP_URL.'/wp-content/pfiles/images/loader.gif'; ?>" /></span><br /><span class="result1"></span>
  <div id="dynamic"></div>
  <div class="spacer"></div>
</div>
<?php
}
else
{
  echo '<div id="container" style="width: 90%; margin: 0 auto; float: none;"><strong>All Comments have been moderated!</strong></div>';
}
}
?>